package cn.tedu.yushougeproduct.controller;

import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushouge.commons.web.JsonResult;
import cn.tedu.yushougeproduct.pojo.param.SkuAddNewParam;
import cn.tedu.yushougeproduct.pojo.param.SkuUpdateInfoParam;
import cn.tedu.yushougeproduct.pojo.param.SpuUpdateInfoParam;
import cn.tedu.yushougeproduct.pojo.vo.SkuListItemVO;
import cn.tedu.yushougeproduct.pojo.vo.SkuStandardVO;
import cn.tedu.yushougeproduct.service.ISkuService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/sku")
@Api(tags = "11. SKU模块")
@Slf4j
@Validated
public class SkuController {

    @Autowired
    ISkuService skuService;

    @PostMapping("/add-new")
    @ApiOperation("添加SKU")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid SkuAddNewParam skuAddNewParam) {
        log.debug("开始处理【添加SKU】的请求，参数：{}", skuAddNewParam);
        skuService.addNew(skuAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改sku")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "skuID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的skuID值！") Long id,
                                           @Valid SkuUpdateInfoParam skuUpdateInfoParam) {
        log.debug("开始处理【修改spu详情】的请求，ID：{}", id);
        skuService.updateInfoById(id, skuUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询SKU详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "skuID", required = true, dataType = "long")
    })
    public JsonResult<SkuStandardVO> getStandardById(@Range(min = 1, message = "根据ID查询SKU详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) throws Exception {
        log.debug("开始处理【根据ID查询SPU完整信息】的请求，参数：{}", id);
        SkuStandardVO result = skuService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @GetMapping("/list-by-spu")
    @ApiOperation("根据SPU查询SKU列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "spuId", value = "spuId", required = true, dataType = "long")
    })
    public JsonResult<PageData<SkuListItemVO>> list(@Range(min = 1, message = "查询SKU失败，请提供正确的页码值！") @RequestParam Long spuId) {
        log.debug("开始处理【根据SPU查询SKU列表】的请求，SPU：{}", spuId);
        PageData<SkuListItemVO> pageData = skuService.listBySpuId(spuId);
        return JsonResult.ok(pageData);
    }


}
